<?php

namespace App\Listeners;

use App\Events\SendNoticeToAll;
use App\Models\Notification;
use App\Models\Notice;
use App\Models\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMsgToAll
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SendNoticeToAll  $event
     * @return void
     */
    public function handle(SendNoticeToAll $event)
    {
        $no_id = $event->id;
        $user_id = User::all();
        $notice = Notice::where(['id'=>$no_id])->first();
        $type = $event->type;
        $title = $notice->title;
        $content = $notice->content;
        $ins = [
            'title' => $title,
            'content' => $content,
            'time' => date('Y-m-d H:i:s'), 
        ]; 
        $ins_en = json_encode($ins);
        foreach($user_id as $value){
            $msg_data[] = [
                'user_id' => $value->id,
                'type' => $type,
                'data' => $ins_en,
                'create_id' => 1,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }
        Notification::insert($msg_data);
    }
}
